﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages
{
    public class Symptom : BasePage
    {
       
        protected override void ShowPage()
        {
            base.ShowPage();
            if(ErrorCount > 0) return;
            
            HandleCommon();
            if(ErrorCount > 0) return;
            
            if(IsPost) HandlePost();
            else HandleGet();
        }

        // 如果用户已经登录，记录当前用户的患病信息
        protected PatientRecordInfo prData;
        protected bool userAddedCurrentSymptom = false;

        protected SymptomInfo SymptomData;
        protected List<DiseaseInfo> DiseaseDataListRelate;
        protected List<UserInfo> PatientDataListRelate;
        protected List<TopicInfo> TopicDataListHottest;
        protected List<TopicInfo> TopicDataListLatest;


        protected int DiseaseHeight = 150;

        protected void HandleCommon()
        {
            
        }
        
        protected void HandlePost()
        {
            
        }
        protected void HandleGet()
        {
            int sid = RequestTools.GetQueryInt("id", 0);
            if (sid == 0)
            {
                this.AddErrLine("找不到症状信息");
                return;
            }

            SymptomData = SymptomBLL.Instance.GetSymptomInfo(sid);

            if (SymptomData.Id <= 0)
            {
                this.AddErrLine("找不到症状信息");
                return;
            }
            this.AddLocation("症状列表", "/SymptomList.aspx");
            this.AddLocation(SymptomData.Name, "/Symptom.aspx?id=" + SymptomData.Id);
            this.SetTitle(SymptomData.Name);

            int n = 8;
            DiseaseDataListRelate = DiseaseBLL.Instance.GetHottestDiseaseBySymptom(sid, n);

            if (UserData.Id > 0)
            {
                PatientDataListRelate = PatientBLL.Instance.GetHottestPatientBySymptomId(sid, n);
                TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(n);
            }
            else
            {
                PatientDataListRelate = PatientBLL.Instance.GetHottestPatientBySymptomId(sid, 2);
                TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(3);
            }

            foreach (DiseaseInfo info in DiseaseDataListRelate)
            {
                UserInfo uinfo = new UserInfo();
                uinfo.Id = info.PatientId;
                uinfo.UserName = info.PatientName;
                uinfo.Avatar = info.PatientAvatar;
                uinfo.DiseaseId = info.Id;
                uinfo.DiseaseName = info.Name;
            }

            TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(n);

            // 用户已经登录
            if (UserData.Id > 0)
            {
                prData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);

                if (prData.Id > 0 && prData.DiseaseId > 0)
                {
                    userAddedCurrentSymptom = GetUserAddedCurrentSymptom();
                }
            }
        }

        public bool GetUserAddedCurrentSymptom()
        {
            List<SymptomHistoryInfo> symptomList;
            symptomList = PatientSymptomBLL.Instance.GetCurrentSymptomList(prData.Id);

            if (symptomList.Count <= 0)
                return false;

            foreach (SymptomHistoryInfo symptomHistInfo in symptomList)
            {
                if (symptomHistInfo.SymptomId == SymptomData.Id)
                    return true;
            }

            return false;
        }
    }
}